﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;

namespace SettingsLib.Ceny
{
    /// <summary>
    /// Interaction logic for UpravitRowWin.xaml
    /// </summary>
    public partial class UpravitRowWin : Window
    {
        public delegate void ValueChangedEventHandler(object sender);
        public event ValueChangedEventHandler OKClicked;

        TextBox[] textBoxes;
        public UpravitRowWin(string id)
        {
            InitializeComponent();
            this.Title = id;
            textBoxes = new TextBox[] { day0, day1, day2,day3,day4,day5,day6,day7,day8};
            loadDataFromDb(id);
        }

        private void loadDataFromDb(string id)
        {
          Dictionary<string,string> row =   DataLib.Settings.databases.dbCeny.QueryGetRow("SELECT * FROM Ceny WHERE prefix='"+id+"'");
          try
          {
              foreach (TextBox txt in textBoxes)
                  txt.Text = row[txt.Name];
          }
          catch { };
        }

        private Dictionary<string,string> GetUIData()
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            foreach (TextBox txt in textBoxes)
            {
                if(txt.Text !=string.Empty)
                result.Add(txt.Name, txt.Text);
            }
            return result;
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            DataLib.Settings.databases.dbCeny.UpdateRow(GetUIData(), "Ceny", "WHERE prefix='" + this.Title + "'");
            if (OKClicked != null)
                OKClicked(this);
            this.Close();
        }
    }
}